using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class CategoriesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class CategoriesDbo : DatabaseObject
        {
            public CategoriesDbo()
            {
                Name = "Categories";
                GeneratedClassName = "Categories";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "smallint", IsIdentity = true, IsNullable = false, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ParentId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "ParentId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Code", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Code" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Name", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 500, Precision = 0, Scale = 0, GeneratedPropertyName = "Name" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Level", ColumnType = "tinyint", IsIdentity = false, IsNullable = false, Length = 1, Precision = 3, Scale = 0, GeneratedPropertyName = "Level" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Convention", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Convention" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryUniverseId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryUniverseId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryRoleId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryRoleId" });
              
            }
        }
		
		public static List<CategoryRow> Select(Expression<Func<CategoryRow, bool>> expression)
        {
            List<CategoryRow> rows = new List<CategoryRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<CategoryRow>(new CategoriesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<CategoryRow> Select()
		{
			List<CategoryRow> rows = new List<CategoryRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<CategoryRow>(new CategoriesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<CategoryRow> PagedSelect(Expression<Func<CategoryRow, bool>> filterExpression, Expression<Func<CategoryRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<CategoryRow> rows = new List<CategoryRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<CategoryRow>(new CategoriesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<CategoryRow> CategoryList)
        {
            CategoryRow row;

            while (reader.Read())
            {
                row = new CategoryRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					ParentId = Util.CastDbValue<int?>(reader["ParentId"]),
					Code = Util.CastDbValue<string>(reader["Code"]),
					Name = Util.CastDbValue<string>(reader["Name"]),
					Level = Util.CastDbValue<int>(reader["Level"]),
					Convention = Util.CastDbValue<string>(reader["Convention"]),
					CategoryUniverseId = Util.CastDbValue<int?>(reader["CategoryUniverseId"]),
					CategoryRoleId = Util.CastDbValue<int?>(reader["CategoryRoleId"])
                };
                                
                CategoryList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(CategoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<CategoryRow>(new CategoriesDbo(), row);
            }
        }

		public static void Update(CategoryRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<CategoryRow>(new CategoriesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<CategoryRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<CategoryRow>(new CategoriesDbo(), expression);
            }
        }

	}
}
	
